import React, { Component } from 'react'
import Child from './Child'

export default class Parent extends Component {
  state = {
    hasError: '' // 用于标识子组件是否携带错误信息
  }
  // 当Parent的子组件发生错误时，会触发getDerivedStateFromError调用，并携带错误信息
  static getDerivedStateFromError(error) {
    console.log(error);
    return { hasError: error }
  }
  render() {
    return (
      <div>
        <h2>我是父组件</h2>
        {this.state.hasError ? <h2>当前网络不稳定，请稍后再试</h2> : <Child /> }
      </div>
    )
  }
}
